package com.nativetalk.service.study.insert;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.course.TdNtCoursePictureList;
import com.nativetalk.bean.course.TdNtCoursePictureListUrl;
import com.nativetalk.bean.course.TsNtCourseInfoDetail;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.course.TdNtMembCourseDetail;
import com.nativetalk.bean.member.setmeal.TdNtMembSetmealDetail;
import com.nativetalk.bean.study.TdNtStudyTeachInsert;
import com.nativetalk.bean.study.TdNtStudyTeachUpdate;
import com.nativetalk.bean.teacher.TdNtTeacherGetui;
import com.nativetalk.bean.teacher.TdNtTeacherState;
import com.nativetalk.bean.teacher.TdNtTeacherStudyInsert;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.getui.AndroidTeacherGetui;
import com.nativetalk.common.getui.IosTeacherGetui;
import com.nativetalk.common.util.*;
import com.nativetalk.dao.TdNtMembSetmealDao;
import com.nativetalk.dao.TdNtStudyTeachDao;
import com.nativetalk.dao.TdNtTeacherDao;
import com.nativetalk.dao.TsNtCourseInfoDao;
import com.nativetalk.service.study.search.StudyTeachServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

/**
 * Created by lizhun on 16/1/7.
 */
@Service
public class StudyTeachInsertServiceImpl implements StudyTeachInsertService {
    private static Logger logger = Logger.getLogger(StudyTeachServiceImpl.class);
    @Resource
    private TdNtStudyTeachDao tdNtStudyTeachDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TsNtCourseInfoDao tsNtCourseInfoDao;
    @Resource
    private TdNtMembSetmealDao tdNtMembSetmealDao;
    @Override
    public RetInfo inserttdNtMembSetmeal(TdNtMember tdNtMember, String cou_id,int version) {
        String logInfo = this.getClass().getName() + ":inserttdNtMembSetmeal:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
        Map<String , TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);
        TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
        try {
            //判断课程是否可用
            if (tsNtCourseInfoDetail != null) {
                Map<String , List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tsNtCourseInfoDetail.getCou_id() + "");
                tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                tdNtStudyTeachInsert.setStudy_cou_picture(tdNtCoursePictureLists.get(0).getImg_url());
                tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                tdNtStudyTeachInsert.setStudy_state("0");
                tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                MemcachedUtils.add(tdNtMember.getToken() + "nativetalk", tdNtStudyTeachInsert.getStt_id(), 2*60);

            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("COURESE.STUDY.RESULT.NULL"));
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        } finally {
            FindTeacher findTeacher = new FindTeacher();
            findTeacher.run(tdNtStudyTeachDao, tdNtStudyTeachInsert.getStt_id(), tsNtCourseInfoDetail.getCate_id(), tdNtTeacherDao
                    , tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(),tsNtCourseInfoDetail.getCou_english_name(),tdNtMember.getEqu_type(),tdNtMember.getEqu_client_id(),1);
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
            if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                Map<String, Object> map = new HashMap<>();
                map.put("memb_name", tdNtMember.getMemb_name());
                map.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                map.put("tea_name", tdNtTeacherIndex.getTea_name());
                map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);
                List<TdNtCoursePictureListUrl> tdNtCoursePictureListUrls = new ArrayList<>();
                for (int i = 0 ; i < tdNtCoursePictureLists.size(); i ++) {
                    TdNtCoursePictureListUrl tdNtCoursePictureListUrl = new TdNtCoursePictureListUrl();
                    tdNtCoursePictureListUrl.setImg_url(tdNtCoursePictureLists.get(i).getImg_url());
                    tdNtCoursePictureListUrls.add(tdNtCoursePictureListUrl);
                }
                if (version >1) {
                    map.put("course_pictures", tdNtCoursePictureLists);
                } else {
                    map.put("course_pictures", tdNtCoursePictureListUrls);
                }

                map.put("channel", tdNtTeacherIndex.getToken());
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("成功");
                retInfo.setObj(map);
                /**
                 * 接通发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (!tdNtTeacherGetuisCached.get(i).getTea_id().equals(tdNtStudyTeachDetail.getTea_id())) {
                            if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                                AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                            } else {
                                IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                            }
                        }


                    }
                }
            } else {
                tdNtStudyTeachDetail.setStudy_state("6");
                tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("STUDY.START.TEACHER.NULL"));
                /**
                 * 取消发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                            AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                        } else {
                            IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                        }

                    }
                }
            }

        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo oneKeyCall(TdNtMember tdNtMember,int version) {
        String logInfo = this.getClass().getName() + ":oneKeyCall:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
        Map<String , TsNtCourseInfoDetail> map = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);


        String[] keys = map.keySet().toArray(new String[0]);
        Random random = new Random();
        String randomKey = keys[random.nextInt(keys.length)];

        TsNtCourseInfoDetail tsNtCourseInfoDetail = map.get(randomKey);
        try {

            //判断课程是否可用
            if (tsNtCourseInfoDetail != null) {
                Map<String , List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tsNtCourseInfoDetail.getCou_id() + "");
                tdNtStudyTeachInsert.setCou_id(Integer.valueOf(tsNtCourseInfoDetail.getCou_id()));
                tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                tdNtStudyTeachInsert.setStudy_cou_picture(tdNtCoursePictureLists.get(0).getImg_url());
                tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                tdNtStudyTeachInsert.setStudy_state("0");
                tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                MemcachedUtils.add(tdNtMember.getToken() + "nativetalk", tdNtStudyTeachInsert.getStt_id(), 2*60);

            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("COURESE.STUDY.RESULT.NULL"));
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        } finally {
            FindTeacher findTeacher = new FindTeacher();
            findTeacher.run(tdNtStudyTeachDao, tdNtStudyTeachInsert.getStt_id(), tsNtCourseInfoDetail.getCate_id(), tdNtTeacherDao
                    , tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(),tsNtCourseInfoDetail.getCou_english_name(),tdNtMember.getEqu_type(),tdNtMember.getEqu_client_id(),1);
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
            if (tdNtStudyTeachDetail != null && tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                Map<String, Object> result = new HashMap<>();
                result.put("memb_name", tdNtMember.getMemb_name());
                result.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                result.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                result.put("tea_name", tdNtTeacherIndex.getTea_name());
                result.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);

                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(tdNtStudyTeachDetail.getCou_id()+"");
                List<TdNtCoursePictureListUrl> tdNtCoursePictureListUrls = new ArrayList<>();
                for (int i = 0 ; i < tdNtCoursePictureLists.size(); i ++) {
                    TdNtCoursePictureListUrl tdNtCoursePictureListUrl = new TdNtCoursePictureListUrl();
                    tdNtCoursePictureListUrl.setImg_url(tdNtCoursePictureLists.get(i).getImg_url());
                    tdNtCoursePictureListUrls.add(tdNtCoursePictureListUrl);
                }
                if (version >1) {
                    result.put("course_pictures",  tdNtCoursePictureLists);
                } else {
                    result.put("course_pictures", tdNtCoursePictureListUrls);
                }

                result.put("channel", tdNtTeacherIndex.getToken());
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip(ValidationMessages.getValue("STUDY.START.SUCCESS"));
                retInfo.setObj(result);
                /**
                 * 接通发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (tdNtTeacherGetuisCached.get(i).getTea_id() != tdNtStudyTeachDetail.getTea_id()) {
                            if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                                AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                            } else {
                                IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                            }
                        }


                    }
                }
            } else {
                tdNtStudyTeachDetail.setStudy_state("6");
                tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("STUDY.START.TEACHER.NULL"));
                /**
                 * 取消发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                            AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                        } else {
                            IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                        }

                    }
                }
            }

        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo inserttdNtMembSetmealByTea_Id(TdNtMember tdNtMember, String cou_id, int tea_id) {
        String logInfo = this.getClass().getName() + ":inserttdNtMembSetmealByTea_Id:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        TdNtTeacherGetui tdNtTeacherGetui = tdNtTeacherDao.findTdNtTeacherByTeaId(tea_id);
        /**
         * 判断老师是否存在及是否在线
         */
        if (tdNtTeacherGetui != null && MemcachedUtils.get(tdNtTeacherGetui.getToken()) != null) {
            TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
            Map<String , TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);
            TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
            try {
                //判断课程是否可用
                if (tsNtCourseInfoDetail != null) {
                    Map<String , List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tsNtCourseInfoDetail.getCou_id() + "");
                    tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                    tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                    tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                    tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                    tdNtStudyTeachInsert.setStudy_cou_picture(tdNtCoursePictureLists.get(0).getImg_url());
                    tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                    tdNtStudyTeachInsert.setStudy_state("0");
                    tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                    tdNtStudyTeachDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                    MemcachedUtils.add(tdNtMember.getToken() + "nativetalk", tdNtStudyTeachInsert.getStt_id(), 2*60);

                } else {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("COURESE.STUDY.RESULT.NULL"));
                }


            } catch (Exception e) {
                retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
                retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
                logger.error(e.getMessage(),e);
            } finally {
                CallTeacher callTeacher = new CallTeacher();
                callTeacher.run(tdNtStudyTeachDao, tdNtStudyTeachInsert.getStt_id(), tsNtCourseInfoDetail.getCate_id(),
                        tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(),tsNtCourseInfoDetail.getCou_english_name(),tdNtTeacherGetui,tdNtMember.getEqu_type(),tdNtMember.getEqu_client_id());
                TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
                if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                    TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                    Map<String, Object> map = new HashMap<>();
                    map.put("memb_name", tdNtMember.getMemb_name());
                    map.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                    map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                    map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                    map.put("tea_name", tdNtTeacherIndex.getTea_name());
                    map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                    Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);
                    map.put("course_pictures", tdNtCoursePictureLists);
                    map.put("channel", tdNtTeacherIndex.getToken());
                    retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                    retInfo.setTip("成功");
                    retInfo.setObj(map);
                } else {
                    tdNtStudyTeachDetail.setStudy_state("6");
                    tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                    tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("STUDY.START.TEACHER.NULL"));
                    /**
                     * 取消发送推送
                     */
                    if (tdNtTeacherGetui.getEqu_type().equals("1")) {
                        AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                    } else {
                        IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                    }
                }

            }
        } else {
            retInfo.setMark(ValidationMessages.getValue("MARKNO"));
            retInfo.setTip("对不起,您呼叫的老师不在线!");
            /**
             * 将老师修改为不在线
             */
            TdNtTeacherState tdNtTeacherState =  new TdNtTeacherState();
            tdNtTeacherState.setTea_id(tea_id);
            tdNtTeacherState.setState(Constants.Teacher.State.offline);
            tdNtTeacherState.setState_order(Constants.Teacher.StateOrder.offline);
            tdNtTeacherDao.updateTdNtTeacherState(tdNtTeacherState);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public RetInfo inserttdNtMembSetmealByTea_IdThree(TdNtMember tdNtMember, String cou_id, int tea_id,Double balance,String start_time,String end_time,String free,Double sutdy_amount,String version) {
        String logInfo = this.getClass().getName() + ":inserttdNtMembSetmealByTea_IdThree:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        TdNtTeacherGetui tdNtTeacherGetui = tdNtTeacherDao.findTdNtTeacherByTeaId(tea_id);
        /**
         * 判断老师是否存在及是否在线
         */
        if (tdNtTeacherGetui != null && MemcachedUtils.get(tdNtTeacherGetui.getToken()) != null) {
            TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
            Map<String , TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);
            TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
            TdNtMembSetmealDetail tdNtMembSetmealDetail = new TdNtMembSetmealDetail();
            try {
                //判断课程是否可用
                if (tsNtCourseInfoDetail != null) {
                    //获取套餐的剩余时间
                    Map<String,Object> map1 = new HashMap<>();
                    map1.put("memb_id",tdNtMember.getMemb_id());
                    map1.put("cou_id",tsNtCourseInfoDetail.getCate_id());
                    tdNtMembSetmealDetail = tdNtMembSetmealDao.findTdNtMembSetmealDetail(map1);
                    if (tdNtMembSetmealDetail!=null) {
                        tdNtMembSetmealDetail.setSet_time(tdNtMembSetmealDetail.getSurplus_time());
                    }
                    Map<String , List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tsNtCourseInfoDetail.getCou_id() + "");
                    tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                    tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                    tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                    tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                    tdNtStudyTeachInsert.setStudy_cou_picture(tdNtCoursePictureLists.get(0).getImg_url());
                    tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                    tdNtStudyTeachInsert.setStudy_state("0");
                    tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                    tdNtStudyTeachDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                    MemcachedUtils.add(tdNtMember.getToken() + "nativetalk", tdNtStudyTeachInsert.getStt_id(), 2*60);

                } else {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("COURESE.STUDY.RESULT.NULL"));
                }


            } catch (Exception e) {
                retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
                retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
                logger.error(e.getMessage(),e);
            } finally {
                TdNtMembCourseDetail tdNtMembCourse =new TdNtMembCourseDetail();
                tdNtMembCourse.setCou_id(Integer.valueOf(cou_id));
                tdNtMembCourse.setMemb_id(tdNtMember.getMemb_id());
                tdNtMembCourse = tdNtStudyTeachDao.findTdNtMembCourseDetail(tdNtMembCourse);
                CallTeacher callTeacher = new CallTeacher();
                callTeacher.run(tdNtStudyTeachDao, tdNtStudyTeachInsert.getStt_id(), tsNtCourseInfoDetail.getCate_id(),
                        tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(),tsNtCourseInfoDetail.getCou_english_name(),tdNtTeacherGetui,tdNtMember.getEqu_type(),tdNtMember.getEqu_client_id());
                TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
                if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                    TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                    Map<String, Object> map = new HashMap<>();
                    if (tdNtMembCourse == null){
                        map.put("studySize", 1);
                    }else{
                        map.put("studySize", tdNtMembCourse.getStudy_page());
                    }
                    if (tdNtMembSetmealDetail!=null) {
                        map.put("surplus_time",tdNtMembSetmealDetail.getSurplus_time());
                    }else{
                        map.put("surplus_time",0);
                    }
                    map.put("free",free);
                    map.put("sutdy_amount",sutdy_amount);
                    map.put("start_time", start_time);
                    map.put("end_time", end_time);
                    map.put("balance", balance);
                    map.put("memb_name", tdNtMember.getMemb_name());
                    map.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                    int ver = Integer.valueOf(version);
                    if(ver >= 4){
                        if ("100".equals(cou_id.toString())){
                            map.put("video_switch",0);
                            map.put("course_flag",0);
                        }else{
                            map.put("video_switch", tdNtMember.getVideo_switch());
                            map.put("course_flag",1);
                        }
                    }else{
                        map.put("video_switch",1);
                        map.put("course_flag",1);
                    }
                    map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                    map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                    map.put("tea_name", tdNtTeacherIndex.getTea_name());
                    map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                    Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                    List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);
                    map.put("course_pictures", tdNtCoursePictureLists);
                    map.put("channel", tdNtTeacherIndex.getToken());
                    retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                    retInfo.setTip("成功");
                    retInfo.setObj(map);
                } else {
                    tdNtStudyTeachDetail.setStudy_state("6");
                    tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                    tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("STUDY.START.TEACHER.NULL"));
                    /**
                     * 取消发送推送
                     */
                    if (tdNtTeacherGetui.getEqu_type().equals("1")) {
                        AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                    } else {
                        IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                tdNtTeacherGetui.getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                    }
                }

            }
        } else {
            retInfo.setMark(ValidationMessages.getValue("MARKNO"));
            retInfo.setTip("对不起,您呼叫的老师不在线!");
            /**
             * 将老师修改为不在线
             */
            TdNtTeacherState tdNtTeacherState =  new TdNtTeacherState();
            tdNtTeacherState.setTea_id(tea_id);
            tdNtTeacherState.setState(Constants.Teacher.State.offline);
            tdNtTeacherState.setState_order(Constants.Teacher.StateOrder.offline);
            tdNtTeacherDao.updateTdNtTeacherState(tdNtTeacherState);
        }

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo inserttdNtMembSetmealTwo(TdNtMember tdNtMember, String cou_id) {
        String logInfo = this.getClass().getName() + ":inserttdNtMembSetmealTwo:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
        Map<String , TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);
        TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
        try {
            //判断课程是否可用
            if (tsNtCourseInfoDetail != null) {
                Map<String , List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tsNtCourseInfoDetail.getCou_id() + "");
                tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                tdNtStudyTeachInsert.setStudy_cou_picture(tdNtCoursePictureLists.get(0).getImg_url());
                tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                tdNtStudyTeachInsert.setStudy_state("0");
                tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                MemcachedUtils.add(tdNtMember.getToken() + "nativetalk", tdNtStudyTeachInsert.getStt_id(), 2*60);

            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("COURESE.STUDY.RESULT.NULL"));
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        } finally {
            FindTeacher findTeacher = new FindTeacher();
            findTeacher.run(tdNtStudyTeachDao, tdNtStudyTeachInsert.getStt_id(), tsNtCourseInfoDetail.getCate_id(), tdNtTeacherDao
                    , tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(),tsNtCourseInfoDetail.getCou_english_name(),tdNtMember.getEqu_type(),tdNtMember.getEqu_client_id(),2);
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
            if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                Map<String, Object> map = new HashMap<>();
                map.put("memb_name", tdNtMember.getMemb_name());
                map.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                map.put("tea_name", tdNtTeacherIndex.getTea_name());
                map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);

                map.put("course_pictures", tdNtCoursePictureLists);


                map.put("channel", tdNtTeacherIndex.getToken());
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("成功");
                retInfo.setObj(map);
                /**
                 * 接通发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (!tdNtTeacherGetuisCached.get(i).getTea_id().equals(tdNtStudyTeachDetail.getTea_id())) {
                            if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                                AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                            } else {
                                IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                            }
                        }


                    }
                }
            } else {
                tdNtStudyTeachDetail.setStudy_state("6");
                tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("STUDY.START.TEACHER.NULL"));
                /**
                 * 取消发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                            AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                        } else {
                            IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                        }

                    }
                }
            }

        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public RetInfo inserttdNtMembSetmealThree(TdNtMember tdNtMember, String cou_id,Double balance,String start_time,String end_time,String free,Double sutdy_amount,String version) {
        String logInfo = this.getClass().getName() + ":inserttdNtMembSetmealThree:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
        Map<String , TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);
        TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(cou_id);
        TdNtMembSetmealDetail tdNtMembSetmealDetail = new TdNtMembSetmealDetail();
        try {
            //判断课程是否可用
            if (tsNtCourseInfoDetail != null) {
                //获取套餐的剩余时间
                Map<String,Object> map1 = new HashMap<>();
                map1.put("memb_id",tdNtMember.getMemb_id());
                map1.put("cou_id",tsNtCourseInfoDetail.getCate_id());
                tdNtMembSetmealDetail = tdNtMembSetmealDao.findTdNtMembSetmealDetail(map1);
                if (tdNtMembSetmealDetail!=null) {
                    tdNtMembSetmealDetail.setSet_time(tdNtMembSetmealDetail.getSurplus_time());
                }
                Map<String , List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tsNtCourseInfoDetail.getCou_id() + "");
                tdNtStudyTeachInsert.setCou_id(tsNtCourseInfoDetail.getCou_id());
                tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                tdNtStudyTeachInsert.setStudy_cou_picture(tdNtCoursePictureLists.get(0).getImg_url());
                tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                tdNtStudyTeachInsert.setStudy_state("0");
                tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                MemcachedUtils.add(tdNtMember.getToken() + "nativetalk", tdNtStudyTeachInsert.getStt_id(), 2*60);

            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("COURESE.STUDY.RESULT.NULL"));
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        } finally {
            TdNtMembCourseDetail tdNtMembCourse =new TdNtMembCourseDetail();
            tdNtMembCourse.setCou_id(Integer.valueOf(cou_id));
            tdNtMembCourse.setMemb_id(tdNtMember.getMemb_id());
            tdNtMembCourse = tdNtStudyTeachDao.findTdNtMembCourseDetail(tdNtMembCourse);
            FindTeacher findTeacher = new FindTeacher();
            findTeacher.run(tdNtStudyTeachDao, tdNtStudyTeachInsert.getStt_id(), tsNtCourseInfoDetail.getCate_id(), tdNtTeacherDao
                    , tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(),tsNtCourseInfoDetail.getCou_english_name(),tdNtMember.getEqu_type(),tdNtMember.getEqu_client_id(),2);
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
            if (tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                Map<String, Object> map = new HashMap<>();
                if (tdNtMembCourse == null) {
                    map.put("studySize", 1);
                } else {
                    map.put("studySize", tdNtMembCourse.getStudy_page());
                }
                if (tdNtMembSetmealDetail!=null) {
                    map.put("surplus_time",tdNtMembSetmealDetail.getSurplus_time());
                }else{
                    map.put("surplus_time",0);
                }
                map.put("free",free);
                map.put("sutdy_amount",sutdy_amount);
                map.put("start_time", start_time);
                map.put("end_time", end_time);
                map.put("balance", balance);
                map.put("memb_name", tdNtMember.getMemb_name());
                map.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                int ver = Integer.valueOf(version);
                if(ver >= 4){
                    if ("100".equals(cou_id.toString())){
                        map.put("video_switch",0);
                        map.put("course_flag",0);
                    }else{
                        map.put("video_switch", tdNtMember.getVideo_switch());
                        map.put("course_flag",1);
                    }
                }else{
                    map.put("video_switch",1);
                    map.put("course_flag",1);
                }
                map.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                map.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                map.put("tea_name", tdNtTeacherIndex.getTea_name());
                map.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(cou_id);

                map.put("course_pictures", tdNtCoursePictureLists);


                map.put("channel", tdNtTeacherIndex.getToken());
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("成功");
                retInfo.setObj(map);
                /**
                 * 接通发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (!tdNtTeacherGetuisCached.get(i).getTea_id().equals(tdNtStudyTeachDetail.getTea_id())) {
                            if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                                AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                            } else {
                                IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                            }
                        }


                    }
                }
            } else {
                tdNtStudyTeachDetail.setStudy_state("6");
                tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("STUDY.START.TEACHER.NULL"));
                /**
                 * 取消发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                            AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                        } else {
                            IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                        }

                    }
                }
            }

        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo oneKeyCallTwo(TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":oneKeyCallTwo:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
        Map<String , TsNtCourseInfoDetail> map = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);


        String[] keys = map.keySet().toArray(new String[0]);
        Random random = new Random();
        String randomKey = keys[random.nextInt(keys.length)];

        TsNtCourseInfoDetail tsNtCourseInfoDetail = map.get(randomKey);
        try {

            //判断课程是否可用
            if (tsNtCourseInfoDetail != null) {
                Map<String , List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tsNtCourseInfoDetail.getCou_id() + "");
                tdNtStudyTeachInsert.setCou_id(Integer.valueOf(tsNtCourseInfoDetail.getCou_id()));
                tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                tdNtStudyTeachInsert.setStudy_cou_picture(tdNtCoursePictureLists.get(0).getImg_url());
                tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                tdNtStudyTeachInsert.setStudy_state("0");
                tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                MemcachedUtils.add(tdNtMember.getToken() + "nativetalk", tdNtStudyTeachInsert.getStt_id(), 2*60);

            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("COURESE.STUDY.RESULT.NULL"));
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        } finally {
            FindTeacher findTeacher = new FindTeacher();
            findTeacher.run(tdNtStudyTeachDao, tdNtStudyTeachInsert.getStt_id(), tsNtCourseInfoDetail.getCate_id(), tdNtTeacherDao
                    , tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(),tsNtCourseInfoDetail.getCou_english_name(),tdNtMember.getEqu_type(),tdNtMember.getEqu_client_id(),2);
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
            if (tdNtStudyTeachDetail != null && tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                Map<String, Object> result = new HashMap<>();
                result.put("memb_name", tdNtMember.getMemb_name());
                result.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                result.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                result.put("tea_name", tdNtTeacherIndex.getTea_name());
                result.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                result.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);

                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(tdNtStudyTeachDetail.getCou_id()+"");

                result.put("course_pictures",  tdNtCoursePictureLists);

                result.put("channel", tdNtTeacherIndex.getToken());
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip(ValidationMessages.getValue("STUDY.START.SUCCESS"));
                retInfo.setObj(result);
                /**
                 * 接通发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (tdNtTeacherGetuisCached.get(i).getTea_id() != tdNtStudyTeachDetail.getTea_id()) {
                            if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                                AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                            } else {
                                IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                            }
                        }


                    }
                }
            } else {
                tdNtStudyTeachDetail.setStudy_state("6");
                tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("STUDY.START.TEACHER.NULL"));
                /**
                 * 取消发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                            AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                        } else {
                            IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                        }

                    }
                }
            }

        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public RetInfo oneKeyCallThree(TdNtMember tdNtMember,Double balance,String start_time,String end_time,String free,Double sutdy_amount,String version) {
        String logInfo = this.getClass().getName() + ":oneKeyCallThree:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        TdNtStudyTeachInsert tdNtStudyTeachInsert = new TdNtStudyTeachInsert();
        Map<String , TsNtCourseInfoDetail> map = (Map<String , TsNtCourseInfoDetail> )MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);


        String[] keys = map.keySet().toArray(new String[0]);
        Random random = new Random();
        String randomKey = keys[random.nextInt(keys.length)];

        TsNtCourseInfoDetail tsNtCourseInfoDetail = map.get(randomKey);
        TdNtMembSetmealDetail tdNtMembSetmealDetail = new TdNtMembSetmealDetail();
        try {
            //判断课程是否可用
            if (tsNtCourseInfoDetail != null) {
                //获取套餐的剩余时间
                Map<String,Object> map1 = new HashMap<>();
                map1.put("memb_id",tdNtMember.getMemb_id());
                map1.put("cou_id",tsNtCourseInfoDetail.getCate_id());
                tdNtMembSetmealDetail = tdNtMembSetmealDao.findTdNtMembSetmealDetail(map1);
                if (tdNtMembSetmealDetail!=null) {
                    tdNtMembSetmealDetail.setSet_time(tdNtMembSetmealDetail.getSurplus_time());
                }
                Map<String , List<TdNtCoursePictureList>> tdNtCourseBannerListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCourseBannerListMap);
                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCourseBannerListMap.get(tsNtCourseInfoDetail.getCou_id() + "");
                tdNtStudyTeachInsert.setCou_id(Integer.valueOf(tsNtCourseInfoDetail.getCou_id()));
                tdNtStudyTeachInsert.setMemb_id(tdNtMember.getMemb_id());
                tdNtStudyTeachInsert.setCou_english_name(tsNtCourseInfoDetail.getCou_english_name());
                tdNtStudyTeachInsert.setStudy_cou_name(tsNtCourseInfoDetail.getCou_name());
                tdNtStudyTeachInsert.setStudy_cou_picture(tdNtCoursePictureLists.get(0).getImg_url());
                tdNtStudyTeachInsert.setStudy_number(CommonUtil.getOrderNub());
                tdNtStudyTeachInsert.setStudy_state("0");
                tdNtStudyTeachInsert.setStart_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.insertTdNtStudyTeach(tdNtStudyTeachInsert);
                MemcachedUtils.add(tdNtMember.getToken() + "nativetalk", tdNtStudyTeachInsert.getStt_id(), 2*60);

            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("COURESE.STUDY.RESULT.NULL"));
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        } finally {
            TdNtMembCourseDetail tdNtMembCourse =new TdNtMembCourseDetail();
            tdNtMembCourse.setCou_id(Integer.valueOf(tsNtCourseInfoDetail.getCou_id()));
            tdNtMembCourse.setMemb_id(tdNtMember.getMemb_id());
            tdNtMembCourse = tdNtStudyTeachDao.findTdNtMembCourseDetail(tdNtMembCourse);
            FindTeacher findTeacher = new FindTeacher();
            findTeacher.run(tdNtStudyTeachDao, tdNtStudyTeachInsert.getStt_id(), tsNtCourseInfoDetail.getCate_id(), tdNtTeacherDao
                    , tdNtMember.getMemb_name(), tdNtMember.getMemb_head_portrait(),tsNtCourseInfoDetail.getCou_english_name(),tdNtMember.getEqu_type(),tdNtMember.getEqu_client_id(),2);
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(tdNtStudyTeachInsert.getStt_id());
            if (tdNtStudyTeachDetail != null && tdNtStudyTeachDetail.getStudy_state().equals("1")) {
                TdNtTeacherStudyInsert tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherStudyInsert(tdNtStudyTeachDetail.getTea_id());
                Map<String, Object> result = new HashMap<>();
                if(tdNtMembCourse == null){
                    result.put("studySize", 1);
                }else{
                    result.put("studySize", tdNtMembCourse.getStudy_page());
                }
                if (tdNtMembSetmealDetail!=null) {
                    result.put("surplus_time",tdNtMembSetmealDetail.getSurplus_time());
                }else{
                    result.put("surplus_time",0);
                }
                result.put("free",free);
                result.put("sutdy_amount",sutdy_amount);
                result.put("start_time", start_time);
                result.put("end_time", end_time);
                result.put("balance", balance);
                result.put("memb_name", tdNtMember.getMemb_name());
                result.put("memb_head_portrait", tdNtMember.getMemb_head_portrait());
                int ver = Integer.valueOf(version);
                if(ver >= 4){
                    if ("100".equals(tsNtCourseInfoDetail.getCou_id().toString())){
                        result.put("video_switch",0);
                        result.put("course_flag",0);
                    }else{
                        result.put("video_switch", tdNtMember.getVideo_switch());
                        result.put("course_flag",1);
                    }
                }else{
                    result.put("video_switch",1);
                    result.put("course_flag",1);
                }
                result.put("stt_id", tdNtStudyTeachDetail.getStt_id());
                result.put("tea_name", tdNtTeacherIndex.getTea_name());
                result.put("tea_id", tdNtStudyTeachDetail.getTea_id());
                result.put("tea_head_portrait", tdNtTeacherIndex.getTea_head_portrait());
                Map<String , List<TdNtCoursePictureList>> tdNtCoursePictureListMap = (Map<String , List<TdNtCoursePictureList>> )MemcachedUtils.get(MemcachedKey.tdNtCoursePictureListMap);

                List<TdNtCoursePictureList> tdNtCoursePictureLists = tdNtCoursePictureListMap.get(tdNtStudyTeachDetail.getCou_id()+"");

                result.put("course_pictures",  tdNtCoursePictureLists);

                result.put("channel", tdNtTeacherIndex.getToken());
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip(ValidationMessages.getValue("STUDY.START.SUCCESS"));
                retInfo.setObj(result);
                /**
                 * 接通发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (tdNtTeacherGetuisCached.get(i).getTea_id() != tdNtStudyTeachDetail.getTea_id()) {
                            if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                                AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                            } else {
                                IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                        tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                            }
                        }


                    }
                }
            } else {
                tdNtStudyTeachDetail.setStudy_state("6");
                tdNtStudyTeachDetail.setEnd_time(new Timestamp(System.currentTimeMillis()));
                tdNtStudyTeachDao.updateTdNtStudyTeachCancel(tdNtStudyTeachDetail);
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("STUDY.START.TEACHER.NULL"));
                /**
                 * 取消发送推送
                 */
                List<TdNtTeacherGetui> tdNtTeacherGetuisCached = (List<TdNtTeacherGetui>)MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui");
                if (tdNtTeacherGetuisCached != null && tdNtTeacherGetuisCached.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuisCached.size() ; i ++) {
                        if (tdNtTeacherGetuisCached.get(i).getEqu_type().equals("1")) {
                            AndroidTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());
                        } else {
                            IosTeacherGetui.Cancel("NativeTalk","Aw shucks! The call was taken by another teacher. Please wait for another student to call.",
                                    tdNtTeacherGetuisCached.get(i).getEqu_client_id(),tdNtStudyTeachDetail.getStt_id());

                        }

                    }
                }
            }

        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
